package com.gourd.notice.mail.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author gourd
 */
@Service
@Slf4j
@RefreshScope
public class MailService {

    @Autowired
    private JavaMailSender javaMailSender;


    @Value("${spring.mail.username}")
    private String from;
    @Value("${spring.mail.senderName}")
    private String fromName;

    /**
     * 发送包含简单文本的邮件
     *
     * @param toMails 接收者
     * @param subject 主题
     * @param text    内容
     */
    public void sendTxtMail(String[] toMails, String subject, String text) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 设置收件人，寄件人
        simpleMailMessage.setTo(toMails);
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(text);
        // 发送邮件
        javaMailSender.send(simpleMailMessage);
        log.info("^o^= 邮件已发送");
    }

    /**
     * 发送包含HTML文本的邮件
     *
     * @param toMails  接收者
     * @param subject  主题
     * @param htmlText html格式内容
     */
    public void sendHtmlMail(String[] toMails, String subject, String htmlText) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
        try {
            mimeMessageHelper.setTo(toMails);
            mimeMessageHelper.setFrom(from, fromName);
            mimeMessageHelper.setSubject(subject);
            // 启用html
            mimeMessageHelper.setText(htmlText, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 发送邮件
        javaMailSender.send(mimeMessage);
        log.info("^o^= 邮件已发送");

    }

    /**
     * 发送包含内嵌图片的邮件
     */
    public void sendAttachedImageMail(String[] toMails, String subject, String htmlImageText) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            // multipart模式
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setTo(toMails);
            mimeMessageHelper.setFrom(from, fromName);
            mimeMessageHelper.setSubject(subject);
            // 启用html
            mimeMessageHelper.setText(htmlImageText, true);
            // 设置imageId
            FileSystemResource img = new FileSystemResource(new File("E:/1.jpg"));
            mimeMessageHelper.addInline("imageId", img);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 发送邮件
        javaMailSender.send(mimeMessage);
        log.info("^o^= 邮件已发送");
    }

    /**
     * 发送包含附件的邮件
     */
    public void sendAttendedFileMail(String[] toMails, String subject, String htmlAttachText) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            // multipart模式
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            mimeMessageHelper.setTo(toMails);
            mimeMessageHelper.setFrom(from, fromName);
            mimeMessageHelper.setSubject(subject);
            // 启用html
            mimeMessageHelper.setText(htmlAttachText, true);
            // 设置附件
            FileSystemResource img = new FileSystemResource(new File("E:/1.jpg"));
            mimeMessageHelper.addAttachment("image.jpg", img);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 发送邮件
        javaMailSender.send(mimeMessage);
        log.info("^o^= 邮件已发送");
    }
}